package com.syqc.monitor.ui.carinfo

import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.syqc.adapter.TabPagerAdapter
import com.syqc.comlib.UserConstant
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityToolbarTabPagerBinding
import com.syqc.monitor.databinding.IncludeTabLayoutBinding
import com.syqc.monitor.databinding.IncludeViewpagerMatchBinding
import com.syqc.utils.Keys

/**
 *@Author Jasper
 *@Time   2021/2/27
 *@Desc  [R.string.title_car_info]
 */
class CarInfoActivity :
    BaseBarActivity<ActivityToolbarTabPagerBinding>(ActivityToolbarTabPagerBinding::inflate) {

    private lateinit var mTabVb: IncludeTabLayoutBinding
    private lateinit var mViewPagerVB: IncludeViewpagerMatchBinding
    private val mViewModel by viewModels<CarInfoViewModel>()

    override fun initView() {
        mViewModel.carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        mTabVb = IncludeTabLayoutBinding.bind(binding.root)
        mViewPagerVB = IncludeViewpagerMatchBinding.bind(binding.root)
        ToolbarHelper.setToolbar(this, R.string.title_car_info)
        mTabVb.tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        initFragments()
    }

    override fun setListener() {

    }

    private fun tabTitles(): MutableList<String> {
        val titles = resources.getStringArray(R.array.car_info_title).toMutableList()
        if (isGzhy) {
            titles.removeAt(1)
            titles.removeAt(1)
        }
        for (num in titles) {

            mTabVb.tabLayout.addTab(mTabVb.tabLayout.newTab())
        }
        return titles
    }

    private val isGzhy: Boolean by lazy { UserConstant.current_ip == "113.105.152.93" }
    private fun initFragments() {
        val fragments = ArrayList<Fragment>()
        if (UserConstant.platformVersion> UserConstant.PLATFORM_TMN){
            fragments.add(CarBaseFragment())
        }else{
            fragments.add(CarBaseFragmentOld())
        }

        if (!isGzhy) {
            fragments.add(SignRemindFragment.instance("0"))
            fragments.add(SignRemindFragment.instance("1"))
        }
        fragments.add(CarPhotosFragment.getInstance("car"))
        fragments.add(CarTmnPhotosFragment.getInstance())
        fragments.add(CarPhotosFragment.getInstance("drv"))

        mViewPagerVB.mViewPager.adapter =
            TabPagerAdapter(supportFragmentManager, fragments, tabTitles())
        mViewPagerVB.mViewPager.currentItem = 0
        mViewPagerVB.mViewPager.offscreenPageLimit = 5
        mTabVb.tabLayout.setupWithViewPager(mViewPagerVB.mViewPager)
    }
}